import { createRouter, createWebHashHistory } from 'vue-router'

import { defaultRoutes as routes, asyncRoutes } from './routes'

export const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
})
// 动态添加路由的方法
export const addRoutes = (menus) => {
  // 是否有新的路由
  let hasNewRoutes = false
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach((e) => {
      // console.log('e===>', e)
      // 如果能在动态路由中找到
      const item = asyncRoutes.find((o) => o.path === e.frontpath)
      if (item && !router.hasRoute(item.path)) {
        router.addRoute('admin', item)
        hasNewRoutes = true
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child)
      }
    })
  }

  findAndAddRoutesByMenus(menus)
  console.log('router====>', router.getRoutes())

  return hasNewRoutes
}
